package com.sx.zongzhi;

import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sx.zongzhi.port.PermissionsResultListener;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.widget.SystemBarTintManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XZY on 2017/11/21.
 */

public abstract class BaseActivity extends AppCompatActivity{

    protected FrameLayout rootContent;
    Toolbar toolbar;
    TextView title_name;
    ImageView title_back,title_Right,title_Right2;
    RelativeLayout rl_image;
    TextView tvRight;
    int barheight = 0;
    public SystemBarTintManager mBaseTintManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mBaseTintManager = new SystemBarTintManager(this);
            mBaseTintManager.setTranslucentStatus(this, true); //设置沉溺式
            mBaseTintManager.setStatusBarTintEnabled(false);    //设置透明
        }
        MyApplication.instance.addActivity(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN );
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setUI(savedInstanceState);
    }

    public abstract void setUI(Bundle savedInstanceState);

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.activity_base);
        rootContent = (FrameLayout) this.findViewById(R.id.content);
        toolbar = (Toolbar) this.findViewById(R.id.toolbar);
        title_name = (TextView) this.findViewById(R.id.tv_title);
        title_back = (ImageView) this.findViewById(R.id.imv_Left);
        rl_image= (RelativeLayout) this.findViewById(R.id.rl_image);
        title_Right = (ImageView) this.findViewById(R.id.imv_Right);
        title_Right2 = (ImageView) this.findViewById(R.id.imv_Right2);
        tvRight=(TextView) this.findViewById(R.id.tv_right);
     //   tvLeft=(TextView) this.findViewById(R.id.tv_Left);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//&& Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
            barheight = BaseUtil.CheckStatusBarHeight(getApplicationContext());
            ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
            int color = getResources().getColor(R.color.style_color);
            View statusBarView = new View(this);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    barheight);
            statusBarView.setBackgroundColor(color);
            contentView.addView(statusBarView, lp);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(getResources().getColor(R.color.style_color));
            }
        }
//        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
//                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        toolbar.setPadding(0, barheight, 0, 0);
        setBack();
        View.inflate(this, layoutResID, rootContent);
    }

    /**
     * 左侧返回
     */
    protected void setBack(){
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseUtil.hintKb(BaseActivity.this);
                finish();
            }
        });

    }
    /**
     * 隐藏标题栏
     */
    public void setGoneTitle() {
        toolbar.setVisibility(View.GONE);
        rootContent.setPadding(0, barheight, 0, 0);
    }

    /**
     * 设置返回隐藏
     */
    protected void setBackHide() {
        title_back.setVisibility(View.GONE);
    }

    /**
     * 设置返回显示
     */
    protected void setBackShow() {
        title_back.setVisibility(View.VISIBLE);
    }

    /**
     * 设置title
     */
    protected void setTitleName(String title) {
        title_name.setText(title);
    }

    /**
     * 设置标题带左侧图片
     * @param title 标题
     * @param resourcesId 左图
     */
    protected void setTitleName(String title,int resourcesId) {
        title_name.setText(title);
        Drawable drawable=getResources().getDrawable(resourcesId);
        drawable.setBounds( 0, 0, drawable.getMinimumWidth(),drawable.getMinimumHeight());
        title_name.setCompoundDrawables(null,null,drawable,null);
    }

    /**
     * 设置title
     */
    protected void setTitleName(int id) {
        title_name.setText(id);
    }

//    /**
//     * 设置左侧文字
//     * @param resourcesId 文字id
//     */
//    protected void setTitleLeft(int resourcesId) {
//        if (tvLeft.getVisibility()!=View.VISIBLE){
//            tvLeft.setVisibility(View.VISIBLE);
//            title_back.setVisibility(View.GONE);
//        }
//      tvLeft.setText(resourcesId);
//    }
//
//    /**
//     * 设置左侧文字
//     * @param str 标题
//     */
//    protected void setTitleLeft(String str) {
//        if (tvLeft.getVisibility()!=View.VISIBLE){
//            tvLeft.setVisibility(View.VISIBLE);
//            title_back.setVisibility(View.GONE);
//        }
//        tvLeft.setText(str);
//    }

    /**
     * 设置左侧图片
     * @param resourcesId 图标id
     */
    protected void setLeftImage(int resourcesId) {
        title_back.setImageResource(resourcesId);
        title_back.setOnClickListener(null);
    }

    /**
     * 设置右侧文字
     */
    protected void setTitleRight(String title) {
        if (tvRight.getVisibility()!=View.VISIBLE){
            tvRight.setVisibility(View.VISIBLE);
        }
        tvRight.setText(title);
    }
    /**
     * 设置右侧文字
     */
    protected void setTitleRight(int resourcesId) {
        if (tvRight.getVisibility()!=View.VISIBLE){
            tvRight.setVisibility(View.VISIBLE);
        }
        tvRight.setText(resourcesId);
    }

    /**
     * 设置右侧图片
     * @param resourcesId 图标id
     */
    protected void setRightImage(int resourcesId) {
        if (rl_image.getVisibility()!=View.VISIBLE){
            rl_image.setVisibility(View.VISIBLE);
        }
        title_Right.setImageResource(resourcesId);
    }

    /**
     * 设置右侧2图片
     * @param resourcesId 图标id
     */
    protected void setRightImage2(int resourcesId) {
        if (title_Right2.getVisibility()!=View.VISIBLE){
            title_Right2.setVisibility(View.VISIBLE);
        }
        title_Right2.setImageResource(resourcesId);
    }

    /**
     * 设置右侧2图片显示
     */
    protected void setRightImage2Show() {

        title_Right2.setVisibility(View.VISIBLE);

    }
    /**
     * 设置右侧2图片隐藏
     */
    protected void setRightImage2Hide() {
        title_Right2.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        MyApplication.instance.removeActivity(this);
        super.onDestroy();
    }

    private int mRequestCode;
    /**权限回调的接口*/
    private PermissionsResultListener mListener;
    /**
     * 其他 Activity 继承 BaseActivity 调用 performRequestPermissions 方法
     *
     * @param permissions 要申请的权限数组
     * @param requestCode 申请标记值
     * @param listener    实现的接口
     */
    protected void performRequestPermissions(String[] permissions, int requestCode,
                                             PermissionsResultListener listener) {
        if (permissions == null || permissions.length < 1) {
            return;
        }
        mRequestCode = requestCode;
        mListener = listener;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //获取未通过的权限列表
            String[] newPermissions = checkEachSelfPermission(permissions);
            if (newPermissions.length > 0) {
                ActivityCompat.requestPermissions(this, newPermissions, mRequestCode);
            } else {
                if (mListener != null) {
                    mListener.onPermissionGranted();
                }
            }
        } else {
            if (mListener != null) {
                mListener.onPermissionGranted();
            }
        }
    }

    /**
     * 检察每个权限是否申请
     *
     * @return 返回未申请的权限
     */
    private String[] checkEachSelfPermission(String... permissions) {
        List<String> newPermissions = new ArrayList<String>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                newPermissions.add(permission);
            }
        }
        return newPermissions.toArray(new String[newPermissions.size()]);
    }

    /**
     * 请求权限的回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == mRequestCode) {
            boolean b=checkEachPermissionsGranted(grantResults);
            if (b) {//全部通过
                mListener.onPermissionGranted();
            } else {// 用户拒绝申请权限
                mListener.onPermissionDenied();
                ToastUtils.showToast(BaseActivity.this, "您拒绝授权,会导致应用无法正常使用可以在系统设置中重新开启权限");
            }
        }

    }

    /**
     * 检查回调结果
     *
     * @param grantResults
     * @return
     */
    private boolean checkEachPermissionsGranted(int[] grantResults) {
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }


}
